//index.js
//获取应用实例
let app = getApp()
import {
  request,
  requestHasToken
} from '../../service/network'

let formatTime = require('../../utils/util.js')
import  { previewImg,previewImgList }  from '../../behavior/previewImg/previewImg.js'
Page({
  behaviors: [previewImg,previewImgList], //  继承behavior.js里面的properties，data，methods
  data: {
    baseurl: App.config.baseApi,
    createrUserOpenidBoolean: false,
    urlid: '',
    showModal: false,
    // nowTime: new Date().toLocaleString(),
    group: {},
    users: []
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onLoad: function (options) {
    let id = ''
    if (JSON.stringify(options) != '{}') {
      id = options.id
    }
    this.setData({
      urlid: id
    })
    //用户没有授权就去登录页
    // wx.getSetting({
    //   success: res => {
    //     if (!res.authSetting['scope.userInfo']) {
    //       this.notLogin()
    //     }
    //   }
    // })


    if (id != '') {
      request({
        url: 'api/nt/go/dutch/get?id=' + id
      }).then((res) => {
        const datagroup = res.data.data
        this.setData({
          'group.id': datagroup.id,
          'group.title': datagroup.title,
          'group.content': datagroup.content,
          'group.groupTime': datagroup.groupTime,
          'group.type': datagroup.type,
          'group.location': datagroup.location == 'null' ? '' : datagroup.location,
          'group.pic': datagroup.pic == 'null' ? '' : datagroup.pic,
          'group.total': datagroup.total,
          'group.openid': datagroup.openid,
          'group.state': datagroup.state,
          'group.examine': datagroup.examine,
        })
        if(this.data.group.examine!='已审核') {
          if(wx.getStorageSync('userInfo').admin==1 ||wx.getStorageSync('userInfo').admin==2 ){
          } else {
            wx.navigateTo({
              url: '/pages/goDutchBefore/goDutchBefore'
            })
            return false
          }
        }

        // console.log(formatTime.formatTime(new Date(), "p"));
        // console.log(Date.parse(new Date()));
        // if (this.loginPage()) {
        //   requestHasToken({
        //     url: 'api/xcx/auth'
        //   }).then((rauthes) => {
        //     if ( rauthes.data.user.xcxopenid== datagroup.openid) {
        //       this.setData({
        //         createrUserOpenidBoolean: true
        //       })
        //     }
        //   })
        // }
        
        if ( wx.getStorageSync('openId') == datagroup.openid) {
          this.setData({
            createrUserOpenidBoolean: true
          })
        }
        setInterval(this.getUserList,20000,datagroup.id)
        this.getUserList(datagroup.id)
      })
    }
  },
  getUserList(dutchid) {
    request({
      url: 'api/nt/go/dutch/dutchUserList',
      data: {
        dutchid: dutchid,
        sort: 'create_time',
        order: 'asc'
      }
    }).then((res) => {
      let userData = res.data.data
      // let currentUsers = this.data.users
      // for (let key in userData) {
      //   currentUsers.push(userData[key])
      // }
      this.setData({
        users: userData
      })
    })
  },
  handleUp1() {
    if(!this.loginPage()) {return false}
    this.setData({
      showModal: true
    })
  },
  handleUp() {
    requestHasToken({
      url: 'api/go/dutchUser/save',
      data: {
        dutchid: this.data.group.id,
      }
    }).then((res) => {
      wx.showToast({
        title: '报名成功!',
        icon: 'success',
        duration: 1000
      })

      const dutchuserid = res.data.id
      requestHasToken({
        url: 'api/go/dutchUser/get',
        data: {
          id: dutchuserid,
        }
      }).then((res3) => {
        let currentUsers = this.data.users
        let dutchusers = res3.data.data
        currentUsers.push({
          dutchid: dutchusers.id,
          openid: dutchusers.openid,
          gname: dutchusers.gname,
          gavatarurl: dutchusers.gavatarurl,
        })
        this.setData({
          users: currentUsers
        })
      })
    })
  },
  handleUpdate(res) {
    // let id = res.currentTarget.dataset.id
    if (this.data.group.id) {
      wx.navigateTo({
        url: '/pages/goDutchCURD/goDutchCURD?id=' + this.data.group.id
      })
    }
  },
  handleAdd(res) {
    wx.navigateTo({
      url: '/pages/goDutchCURD/goDutchCURD'
    })
  },
  handleUsersDel(event) {
    const { id } = event.detail
    let users = this.data.users.filter((item, index, arr) => {
      if (item.id != id) {
        return item
      }
    });
    this.setData({
      users
    })
  },
  copy() {
    // this.data.group
    // this.data.users
    let aa = ''
    this.data.users.forEach((v,i, arr)=> {
      aa = aa+''+(i+1)+'. '+v.gname+' \n'
    })
    let data = `标题： ${this.data.group.title}
${this.data.group.content }
时间：${this.data.group.groupTime}
人数：${this.data.users.length}
${aa}`
    wx.setClipboardData({
      data: data,
      success: function (res) {
      }
    })
  },
  navigatorIndex() {
    this.loginPage()
    // wx.navigateTo({
    //   url: '/pages/goDutch/goDutch?title=你好,哈哈'
    // })
  },
  navigatorBefore() {
    wx.navigateTo({
      url: '/pages/goDutchBefore/goDutchBefore'
    })
  },
  loginPage() {
    if (!wx.getStorageSync('token') || wx.getStorageSync('token').trim() == "") {
      // wx.navigateTo({
      //   url: '/pages/goDutchBefore/goDutchBefore'
      // })
      wx.redirectTo({
        url: '/pages/login/login?url=/pages/goDutch/goDutch'
      })
      return false
    } else {
      return true
    }
  },
  onShareAppMessage: function () {
    const title = this.data.group.title
    return {
      title,
      path: '/pages/goDutch/goDutch?id=' + this.data.group.id,
      success(e) {
        // shareAppMessage: ok,
        // shareTickets 数组，每一项是一个 shareTicket ，对应一个转发对象
        // 需要在页面onLoad()事件中实现接口
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail(e) {
        // shareAppMessage:fail cancel
        // shareAppMessage:fail(detail message) 
      },
      complete() {}
      /* imageUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590592186298&di=d57bfd9af359d19796c5c8abff956448&imgtype=0&src=http%3A%2F%2F01.minipic.eastday.com%2F20161215%2F20161215154634_940b4d487aedf8f5c0931f7f3591781b_1.jpeg' */
    }
  },

})